﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using VersionComparer.DataModel.Actions;
using VersionComparer.Images;

namespace VersionComparer.DataModel.Xml
{
    [ActionAttribute("Paste xml", 50,  System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.V)]
    public class PasteXmlAction : SpecificAction
    {

        public override bool MayBeExecuted(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            //for now it is off
            return false;
            /*if (Clipboard.ContainsText() || Clipboard.ContainsFileDropList ())
                return true;

            return false;
             * */
        }
        protected override void ExecuteAction(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            List<string> filesToAdd = new List<string>();
            if (Clipboard.ContainsText())
            {
                string text = Clipboard.GetText();
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.LoadXml(text);
                }
                catch
                {
                    return;
                }
                string filename = Path.GetTempFileName();
                File.WriteAllText(filename, text);
                filesToAdd .Add ( filename );
            }
            if (Clipboard.ContainsFileDropList())
            {
                foreach (string file in Clipboard.GetFileDropList())
                {
                    filesToAdd.Add(file);
                }
            }
            if (filesToAdd.Count > 0)
                mainForm.AddItemsToSelection(filesToAdd.ToArray());
        }

        public override System.Drawing.Image Image(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            return VersionComparer.Images.ImageResources.Paste_icon;
        }
    }
}
